package com.yungu.swift.system.sys.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.service.impl.BaseServiceImpl;
import com.yungu.swift.system.sys.dao.SysUserMapper;
import com.yungu.swift.system.sys.model.dto.SysResourceDto;
import com.yungu.swift.system.sys.model.dto.SysUserDto;
import com.yungu.swift.system.sys.model.param.AdminUserAddParam;
import com.yungu.swift.system.sys.model.param.AdminUserResetPasswordParam;
import com.yungu.swift.system.sys.service.SysUserService;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.Md5Utils;
import com.yungu.swift.utils.StringUtils;
import com.yungu.swift.utils.ValidateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.yungu.swift.system.sys.model.enums.SysErrorEnum.ACCOUNT_REPACT;
import static com.yungu.swift.system.sys.model.enums.SysErrorEnum.MOBILE_ILLEGAL;


/**
 * 系统用户操作类
 */
@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUserDto> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    protected IMapper<SysUserDto> getMapper() {
        return sysUserMapper;
    }

    @Override
    public ResponseData<List<SysResourceDto>> queryUserAuthority(String userUuid) {
        return ResponseData.buildSuccessResponse(sysUserMapper.queryUserAuthority(userUuid));
    }

    @Override
    public ResponseData<Boolean> resetPassword(AdminUserResetPasswordParam resetPasswordParam) {
        Map<String, Object> build = MapUtils.build(4);
        build.put("userAccount", resetPasswordParam.getUserAccount());
        build.put("password", Md5Utils.encode(resetPasswordParam.getOldPassword()));
        List<SysUserDto> list = sysUserMapper.list(build);
        if (CollectionUtils.isEmpty(list)) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "账户原密码错误");
        }
        SysUserDto userDto = list.get(0);
        //避免更新多条没必要字段
        SysUserDto editUser = new SysUserDto();
        editUser.setUuid(userDto.getUuid());
        editUser.setPassword(Md5Utils.encode(resetPasswordParam.getNewPassword()));
        editUser.setResetPasswordDate(new Date());
        editUser.setFirstLogin(1);
        return edit(editUser);
    }

    @Override
    public ResponseData<Boolean> add(AdminUserAddParam userAddParam) {
        //验证账号唯一性
        ResponseData<List<SysUserDto>> responseData = list(MapUtils.build("userAccount",
                userAddParam.getUserAccount()));
        if (responseData.isSuccess() && CollectionUtils.isNotEmpty(responseData.getData())) {
            return ResponseData.buildErrorResponse(ACCOUNT_REPACT.getCode(), ACCOUNT_REPACT.getMsg());
        }
        if (!ValidateUtils.isChinaPhoneLegal(userAddParam.getUserMobile())) {
            return ResponseData.buildErrorResponse(MOBILE_ILLEGAL.getCode(), MOBILE_ILLEGAL.getMsg());
        }
        SysUserDto addUserDto = new SysUserDto();
        BeanUtils.copyProperties(userAddParam, addUserDto);
        addUserDto.setUuid(StringUtils.buildUUID());
//        addUserDto.setPassword(Md5Utils.encode("Xdkj123456*"));
        addUserDto.setPassword(Md5Utils.encode(userAddParam.getPassword()));
        addUserDto.setUserType(SysUserDto.USER_TYPE_SUB);
        addUserDto.setStatus(SysUserDto.STATUS_NORMAL);
        addUserDto.setFirstLogin(0);
        addUserDto.setResetPasswordDate(new Date());
        addUserDto.setAppid(userAddParam.getAppid());
        addUserDto.setAgentUuid(userAddParam.getUserAgentUuid());
        addUserDto.setCompanyUuid(userAddParam.getUserCompanyUuid());
        addUserDto.setCreateBy(userAddParam.getUserUuid());
        addUserDto.setCreateOn(new Date());
        return add(addUserDto);
    }
}
